/**
 * Created by Administrator on 2021/2/24 0024.
 */
$(function () {
    init_latecount_css();

    init_latecount_event();
});

function init_latecount_event() {
    // 拖拽上传
    var dz = $('.file-content')[0];
    dz.ondragover = function(ev) {
        //阻止浏览器默认打开文件的操作
        ev.preventDefault();
    }
    dz.ondrop = function (ev) {
        //阻止浏览器默认打开文件的操作
        ev.preventDefault();
        console.log(ev);
        var files = ev.dataTransfer.files;
        uploadFile(files);
    }

    // 添加文件按钮
    $('.addFile').click(function() {
        $('.title-footer').append('<form id="fileupload" enctype="multipart/form-data"><input type="file" name="file" id="file" style="display: none" accept=".xls,.xlsx"/></form>');
        $('#file').click();
    });

    // 文件域点击
    $('.file-content').click(function() {
        $('.title-footer').append('<form id="fileupload" enctype="multipart/form-data"><input type="file" name="file" id="file" style="display: none" accept=".xls,.xlsx"/></form>');
        $('#file').click();
    });

    // 有文件时进行上传
    $('.oper-content').delegate('#file', "change", function() {
        var files = this.files;
        console.log(files);

        uploadFile(files);
    })
}

function init_latecount_css() {
    $('.file-content').mousemove(function() {
        $('.con_title_first span').css("color","#1E90FF");
    });
    $('.file-content').mouseout(function() {
        $('.con_title_first span').css("color","#FFFFFF");
    });
}

function uploadFile(files) {
    if (files.length > 0) {
        var filename = files[0].name;
        var filePattern = filename.substring(filename.lastIndexOf("."), filename.length);

        var param = new FormData();
        param.append("file", files[0]);
        param.append("filename", files[0].name)

        if (filePattern == '.xls' || filePattern == '.xlsx') {
            sendFileRequest("excel/latecount", param, function(e) {
                // BeforeSend 方法
                $('.loading').show();
            }, function(data) {
                // success 方法
                console.log(data);
                $('.loading').hide();
                $('#page-wrapper').load('views/attendance/latecount-success', function() {
                    $(':hidden[name="filepath"]').val(data.object);
                    $(':hidden[name="backurl"]').val("views/attendance/latecount");
                });
            });
        } else {
            toastr.error("请选择Excel文档！")
        }
    } else {
        console.log('invaild select');
    }
}